package com.heyun.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.heyun.bean.HeyunDept;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunSalesPre;
import com.heyun.bean.Message;
import com.heyun.service.impl.SalesPreServiceImpl;

@Controller
@RequestMapping("/SalesPre")
public class SalesPreController {
	@Autowired
	private SalesPreServiceImpl spsi;

	// 方法1 查出所有部门名称集合
	@RequestMapping("/getDeptName")
	@ResponseBody
	public Message getDeptName() {

		List<HeyunDept> deptlist = spsi.getDeptName();
		for (HeyunDept dept : deptlist) {
			System.out.println(dept.getDeptName());
		}
		return Message.success().add("deptlist", deptlist);

	}

	// 方法2 根据选择的部门id动态生成客户数量和部门规模
	@RequestMapping("/getCustomsAndPersons")
	@ResponseBody
	public Message getCustomsAndPersons(int deptId) {
		
		System.out.println("============" + deptId);
		HeyunDept hyd = spsi.getCustomsAndPersons(deptId);
		
		return Message.success().add("hyd", hyd);

	}
	//方法3 添加销售预测
	@RequestMapping("/saveSalesPre")
	@ResponseBody
	public Message saveSalesPre(HeyunSalesPre hsp,@RequestParam("datafile") MultipartFile datafile,HttpSession session){
		System.out.println("==============进入销售预测添加页面==============");
	   //由于取到的部门下拉框的值是id,而数据库需要存储部门名称,所以一下代码用于根据部门id取部门名称
		String deptIds=hsp.getPreDeptName();
		int deptId=0;
		if(deptIds!=null){
			deptId=Integer.parseInt(deptIds);
		}
		//1 得到部门名称
		HeyunDept hyd = spsi.getCustomsAndPersons(deptId);
		String preDeptName=hyd.getDeptName();
		
		//2 得到存储数据文件 的真实路径
	    String path=session.getServletContext().getRealPath("sales_pre_datafile");
	    //3 得到文件的上传后的全路径,存储在数据库中
	    String preDataFile=path+"/"+datafile.getOriginalFilename();
	    System.out.println("文件上传的全路径是======="+preDataFile);
	    //4 获取预测生成时间
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String genTime = sdf.format(System.currentTimeMillis());
		System.out.println("预测生成的时间是========="+genTime);
		//5 获取登陆的用户名,作为销售预测的操作人
		HeyunEmp emp=(HeyunEmp) session.getAttribute("user");
		String operator=emp.getEmpRealName();
		System.out.println("操作人是============"+operator);
	    //将获取到的部门名称/文件全路径/预测生成时间放入到对象中传入后台
	    hsp.setPreDataFile(preDataFile);
	    hsp.setPreDeptName(preDeptName);
	    hsp.setGenTime(genTime);
	    hsp.setOperator(operator);
	    spsi.saveSalesPre(hsp);
		try {
			FileUtils.copyInputStreamToFile(datafile.getInputStream(), new File(path+"/"+datafile.getOriginalFilename()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;       
		}
  //方法4 获取销售预测的数据,用于页面展示
	@RequestMapping("/getSalesPre")
	@ResponseBody
	public Message getSalesPre(int currentPage){
		
		Map<String, Object> map = spsi.getSalesPre(currentPage);
		return Message.success().add("map", map);
		
	}
    //方法4 根据年月查询销售预测
		@RequestMapping("/findSalesPreByMonth")
		@ResponseBody
		public Message findSalesPreByMonth(int currentPage,String preMonth){
            System.out.println("当前页是====="+currentPage+"要查询的月份是====="+preMonth);
			Map<String, Object> map = spsi.findSalesPreByMonth(currentPage, preMonth);
			return Message.success().add("map", map);
			
		}
	//方法5 根据id查看销售预测
		@RequestMapping(value = "findSalesPreById/{salesPreId}", method = RequestMethod.GET)
		@ResponseBody
		public Message findSalesPreById(@PathVariable("salesPreId") Integer salesPreId){
			System.out.println("====================");
			System.out.println("销售预测的id是====="+salesPreId);
			HeyunSalesPre hsp=spsi.findSalesPreById(salesPreId);
			return Message.success().add("hsp", hsp);
			
		}
}
